package ModelLayer;

import java.util.ArrayList;

/**
 * This class represents a specific type of tool, and acts as a container for those tools.
 * 
 * @author Group 4, dm82
 * @version 06-12-12
 */
public class ToolDescription
{
    private ArrayList<Tool> toolList;
    private long    descriptionID;
    private String  description;
    private double  price;
    /**
     * Constructor for objects of class ToolDescription.
     * 
     * @param   descriptionID   The id number of this description. As a long.
     */
    public ToolDescription(long descriptionID, String description, double price)
    {
        this.descriptionID = descriptionID;
        this.description = description;
        this.price = price;
        toolList = new ArrayList<Tool>();
    }
    
    /**
     * This private method returns the index of a tool.
     * 
     * @param   sn  The serialNumber of the tool. as a long.
     * 
     * @return  The index of the Tool, -1 if does not exist.
     */
    private int getToolIndex(long sn)
    {
        int index = -1;
        int i = 0;
        boolean found = false;
        while(!found && i<toolList.size())
        {
            if(toolList.get(i).getSerialNumber() == sn)
            {
                index = i;
                found = true;
            }
            i++;
        }
        return index;
    }
    
    /**
     * This method returns the amount of tools with this description
     * 
     * @reutrn The amount of tools. As an int
     */
    public int getAmount()
    {
        return toolList.size();
    }
    /**
     * This method adds a new tool.
     * 
     * @param   tool    The new tool.
     */
    public void addTool(Tool tool)
    {
        toolList.add(tool);
    }
    
    /**
     * This method returns a tool.
     * 
     * @param   serialNumber    The serial number. As a long.
     * 
     * @return  The tool. As a tool.
     */
    public Tool getTool(long serialNumber)
    {
        Tool tool;
        try
        {
            tool = toolList.get(getToolIndex(serialNumber));
        }
        catch(Exception e)
        {
            tool = null;
        }
        return tool;
    }
    
    /**
     * This method deletes a tool.
     * 
     * @param   serialNumber    The serial number of the tool.
     */
    public void deleteTool(long serialNumber)
    {
        try
        {
            toolList.remove(getToolIndex(serialNumber));
        }
        catch(Exception e){}
    }
    
    /**
     * This method returns the discription ID.
     * 
     * @return  The description id. As a long.
     */
    public long getDescriptionID(){return descriptionID;}
    
    /**
     * This methdo returns a description.
     * 
     * @return  A descript, as a String.
     */
    public String getDescription(){return description;}
    
    /**
     * This method returns a price.
     * 
     * @return  The price.
     */
    public double getPrice(){return price;}
    
    /**
     * this method changes the description.
     * 
     * @param   description     The new description.
     */
    public void setDescription(String description){this.description = description;}
    
    /**
     * This method changes a price.
     * 
     * @param   price   The price.
     */
    public void setPrice(double price){this.price = price;}
    
    /**
     * This method returns a list of tool associated with the tool.
     * 
     * @return a list of tools.
     */
    public ArrayList<Tool> getToolList()
    {
        return toolList;
    }
}
